package com.lsj.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lsj.core.exception.CustomerException;
import com.lsj.core.response.CommonResult;
import com.lsj.core.util.ExceptionHelperUtil;
import com.lsj.service.impl.JwtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:lin.sj
 * @Data:2021/7/6 22:06
 * @Desc:
 */
@RestController
@Api(tags = "登录认证")
public class JwtController {

    @Autowired
    JwtService jwtService;

    @PostMapping("/authentication")
    @ApiOperation(value = "登录",notes = "入参分别为username和password")
    public CommonResult login(@RequestBody Map<String,String> param){
        String username = param.get("username");
        String password = param.get("password");
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            return CommonResult.error("用户名和密码不能为空");
        }
        try {
            return CommonResult.success(jwtService.login(username, password));
        } catch (CustomerException e) {
            return CommonResult.error(e.getMessage());
        }
    }

    /**
     * @Author: lin.sj
     * @Date: 2021/7/6 22:31
     * @Desc: 通过请求头携带的token来换取新的token
     **/
    @PostMapping("/refreshToken")
    @ApiOperation(value = "token刷新")
    public CommonResult refreshToken(@RequestBody Map<String,String> param){
        String refreshToken = param.get("refreshToken");
        ExceptionHelperUtil.nullObjException(refreshToken,"refreshToken不能卫东");
        try {
            return CommonResult.success(jwtService.refreshToken(refreshToken));
        } catch (CustomerException e) {
            return CommonResult.error(e.getMessage());
        }
    }



}
